<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        var str =
            "月固定费: 166.00; --基本套餐费: 166.00; 增值业务费: 12.00; --增值业务-内蒙古联通秘书: 3.00; --增值业务-沃酷狗: 9.00; --增值业务-绿色邮箱: 0.00; 调增减项: 37.00; --补收: 37.00; 调增减项: 189.00; --补收: 189.00; 消费合计:215.00;抵扣合计:0.00;实际应缴合计:215.00;"


        function getJsonFromStr(str) {
            str = str.split(';');
            let detail_list = [];
            let total_list = [];
            let index = 0;

            function split_key_val(str) {
                let obj = {
                    _name: '',
                    _val: ''
                };
                let attr = str.split(':');
                [obj._name, obj._val] = [attr[0], attr[1]];
                // console.log(attr[0], attr[1]);
                return obj;

            }
            for (let i = 0; i < str.length; i++) {
                var val = {};
                if (str[i].trim().indexOf('--') > -1) {
                    detail_list[index].details.push(split_key_val(str[i]));
                } else if (str[i].trim().indexOf('合计') > -1) {
                    total_list.push({
                        'total': split_key_val(str[i]),
                        'details': []
                    })
                } else {
                    detail_list.push({
                        'total': split_key_val(str[i]),
                        'details': []
                    })
                    index = detail_list.length - 1;
                }
            }
            console.log(total_list);
            console.log(detail_list);
        }
        getJsonFromStr(str)
    </script>
</body>

</html>
